package net.godbirds.api.micros;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.godbirds.Constant;
import net.godbirds.aps.micros.MicrosService;
import net.godbirds.domain.model.TDefMicros;
import net.godbirds.domain.model.TDefUser;

/**
 * 微服务控制其
 * @author godbi
 *
 */
@RestController
@RequestMapping(value="/micros")
public class MicrosController {

	@Autowired
	MicrosService microsService;
	

	@RequestMapping(value="servicesUrl",
			method=RequestMethod.GET)
	public ModelAndView servicesUrl() {
		ModelAndView mav = new ModelAndView("service");
		return mav;
	}
	@RequestMapping(value="baseUrl",
			method=RequestMethod.GET)
	public ModelAndView baseUrl() {
		ModelAndView mav = new ModelAndView("manage/baseManage");
		return mav;
	}
	@RequestMapping(value="pubUrl",
			method=RequestMethod.GET)
	public ModelAndView pubUrl() {
		ModelAndView mav = new ModelAndView("manage/pubManage");
		return mav;
	}
	@RequestMapping(value="queryUrl",
			method=RequestMethod.GET)
	public ModelAndView queryUrl() {
		ModelAndView mav = new ModelAndView("manage/queryManage");
		return mav;
	}
	
	@RequestMapping(value="getMicros",
			method=RequestMethod.GET,
			produces="application/json;charset=utf-8")
	public Map<String,Object> getMicroService(HttpServletRequest request){
		TDefUser tUser = (TDefUser) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		Map<String,Object> rtvMap = new HashMap<String,Object>();
		if(tUser == null) {
			rtvMap.put("msg", "登录超时，请重新登录");
			return rtvMap;
		}
		List<TDefMicros> tMicros = microsService.getMicros(tUser);
		if(tMicros == null || tMicros.size() <= 0) {
			rtvMap.put("msg", "当前无服务可用！");
			return rtvMap;
		}
		rtvMap.put("micros", tMicros);
		return rtvMap;
	}
}
